package com.jiali.backend.controller;

import cn.hutool.core.lang.UUID;
import com.jiali.backend.Utils.FileUploadUtil;
import com.jiali.backend.common.ResponseResult;
import com.jiali.backend.controller.request.LoginRequest;
import com.jiali.backend.controller.request.RegisterRequest;
import com.jiali.backend.controller.request.UserInfoUpdateRequest;
import com.jiali.backend.model.User;
import com.jiali.backend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Objects;
import java.util.Set;


@RestController
@RequestMapping("/user")
public class UserController {
    @Value("${file.upload-dir}")
    private String uploadDir;
    @Resource
    private UserService userService;
    /**
     * 登录
     */
    @PostMapping("/login")
    public ResponseResult<User> login(@RequestBody LoginRequest loginRequest, HttpServletRequest request) {
        return userService.userLogin(loginRequest, request);
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public ResponseResult<User> register(@RequestBody RegisterRequest registerRequest) {
        return userService.userRegister(registerRequest);
    }

    /**
     * 登出
     */
    @PostMapping("/logout")
    public ResponseResult<User> logout(HttpServletRequest request) {
        return userService.userLogout(request);
    }

    /**
     * 鉴别登录状态
     */
    @GetMapping("/current")
    public ResponseResult<User> getCurrentUser(HttpServletRequest request) {
        return userService.getCurrentUser(request);
    }

    /**
     * 更新信息
     */
    @PostMapping("/update")
    public ResponseResult<User> updateUserInfo(@RequestBody UserInfoUpdateRequest userInfoUpdateRequest, HttpServletRequest request) {
        return userService.updateUserInfo(userInfoUpdateRequest, request);
    }
    //上传头像
    @PostMapping("/updateAvatar")
    public ResponseResult<User> updateAvatar(
            @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        try {
            // 直接处理文件上传逻辑
            String contentType = file.getContentType();
            if (!"image/jpeg".equals(contentType)
                    && !"image/png".equals(contentType)
                    && !"image/gif".equals(contentType)) {
                return ResponseResult.fail(400, "只支持JPEG/PNG/GIF格式的图片", null);
            }

            // 调用工具类保存文件
            String filename = FileUploadUtil.saveFile(file, uploadDir+"/avatar/"
            );
            String avatarUrl = "http://localhost:8080/upload/avatar/" + filename;

            // 构建更新请求
            UserInfoUpdateRequest updateRequest = new UserInfoUpdateRequest();
            updateRequest.setAvatarUrl(avatarUrl);

            // 更新用户信息
            return userService.updateUserInfo(updateRequest, request);
        } catch (Exception e) {
            return ResponseResult.fail(500, "头像更新失败: " + e.getMessage(), null);
        }
    }

}
